DropoutGrad ================= 计算 Dropout 的反向梯度。对每个元素: .. math:: dx_i = dy_i \cdot mask_i \cdot scale 其中 dy 为上游梯度,mask 与 scale 须与前向 Dropout 一致 (通常 scale 取 1/(1-p),p 为丢弃概率)。 输入: - **input** - 上游梯度地址(dy) - **scale** - 缩放因子(标量) - **length** - 元素个数 - **mask** - 前向掩码地址,长度 ``length`` - **core_mask** - 核掩码(仅共享存储版本) 输出: - **output** - 输出梯度地址(dx),长度 ``length`` 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 支持 fp32 - MT7004 支持 fp16、fp32 **共享存储版本:** .. c:function:: void hp_dropout_grad_s(float16 *input, float16 scale, int length, float16 *output, float16 *mask, int core_mask) .. c:function:: void fp_dropout_grad_s(float *input, float scale, int length, float *output, float *mask, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 17 // MT7004 示例(共享存储多核,DDR 地址) void TestDropoutGradSMCFp32(int length, float scale, int core_mask) { int core_id = get_core_id(); int logic_core_id = GetLogicCoreId(core_mask, core_id); int core_num = GetCoreNum(core_mask); float *input = (float *)0x81000000; float *output = (float *)0x82000000; float *mask = (float *)0x83000000; int i; if (logic_core_id == 0) { for (i = 0; i < length; i++) { input[i] = (float)(i % 100) / 10.0f - 5.0f; mask[i] = (i % 2 == 0) ? 1.0f : 0.0f; } } sys_bar(0, core_num); fp_dropout_grad_s(input, scale, length, output, mask, core_mask); } void main() { int length = 1024; float scale = 0.5f; int core_mask = 0b1111; TestDropoutGradSMCFp32(length, scale, core_mask); } **私有存储版本:** .. c:function:: void hp_dropout_grad_p(float16 *input, float16 scale, int length, float16 *output, float16 *mask) .. c:function:: void fp_dropout_grad_p(float *input, float scale, int length, float *output, float *mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 11 // MT7004 示例(私有存储单核,AM 地址) void TestDropoutGradAMFp32(int length, float scale) { float *input = (float *)0x10010000; float *output = (float *)0x10020000; float *mask = (float *)0x10030000; int i; for (i = 0; i < length; i++) { input[i] = (float)(i % 100) / 10.0f - 5.0f; mask[i] = (i % 2 == 0) ? 1.0f : 0.0f; } fp_dropout_grad_p(input, scale, length, output, mask); } void main() { int length = 1024; float scale = 0.5f; TestDropoutGradAMFp32(length, scale); }